// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import Qt5Compat.GraphicalEffects

import Fk
import Fk.Components.Common

Item {
  id: root
  property string skill
  property var extra_data
  property var choices : (extra_data !== undefined) ? extra_data.choices : []
  property var all_choices : (extra_data !== undefined) ? extra_data.all_choices : []
  property string default_choice : (extra_data !== undefined) ? extra_data.default_choice : ""
  property string answer: default_choice

  width: img.width
  height: 30

  BorderImage {
    id: img
    height: root.height
    width: Math.max(imgText.width + 50, 83)
    border.left: 27
    border.top: 0
    border.right: 27
    border.bottom: 0
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/interaction.png"
  }

  Glow {
    id: imgTextGlow
    source: imgText
    anchors.fill: imgText
    color: '#000000'
    spread: 0.9
    radius: 1.5
  }

  Text {
    id: imgText
    anchors.horizontalCenter: img.horizontalCenter
    anchors.verticalCenter: img.verticalCenter
    font.family: "SimSun"
    font.pixelSize: 14
    color: "white"
    height: contentHeight
    width: contentWidth
    text: {
      const prompt = Util.processPrompt(answer);
      const clip_loc = 7;
      if (prompt.length > clip_loc) {
        return prompt.substring(0, clip_loc) + "..."
      } else {
        return prompt
      }
    }
  }

  MouseArea {
    anchors.fill: root
    onPressed: {
      root.clicked();
    }
  }

  onAnswerChanged: {
    if (!answer) return;
    Lua.call("UpdateRequestUI", "Interaction", "1", "update", answer);
  }

  function clicked() {
    if (choices.length < 2 && choices.includes(answer)) return;
    roomScene.popupBox.sourceComponent =
      Qt.createComponent("../../pages/CardNamesBox.qml");

    const box = roomScene.popupBox.item;
    box.all_names = all_choices;
    box.card_names = choices;
    box.prompt = skill;
    box.accepted.connect(() => {
      answer = box.result;
    });
  }

}
